# -*- coding: utf-8 -*-
# time: 2025/4/9 11:42
# file: ch01.py
# author: hanson
from langchain.agents import create_tool_calling_agent, AgentExecutor
from langchain.tools import Tool
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_ollama import ChatOllama
from langchain_community.tools import DuckDuckGoSearchRun
from langchain.agents import tool

# 自定义工具
@tool
def word_counter(text: str) -> dict:
    """统计文本中的单词数量"""
    return {"word_count": len(text.split())}

# 配置代理
tools = [DuckDuckGoSearchRun(), word_counter]
agent = create_tool_calling_agent(
    llm=ChatOllama(model="qwen2.5:7b"),
    tools=tools
)

# 执行复杂任务
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
result = agent_executor.invoke({
    "input": "搜索LangChain的最新版本号，然后统计'hello world'的单词数"
})